home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / ccompile.zip / BUF128.A next >
Text File  |  1988-11-29  |  3KB  |  159 lines

  1. ;
  2. ; buf128:  a 128 character type-ahead buffer for the IBM-PC
  3. ;
  4. ; This program works by intercepting the calls to the BIOS
  5. ; interrupt routines at 9 for the keystroke interrupts and
  6. ; 16H for the program requests
  7. ; Everything is kept in CS.
  8. ;
  9. ; Caution: buf128 must be linked with the -A option, e.g.
  10. ; BIND B:BUF128 -A
  11.  
  12.         cseg
  13.         public    main_,key_int,request,buffer,head,tail
  14.  
  15. KEYINT:        equ    24H        ; int 9 vector offset
  16. REQINT:        equ    58H        ; int 16H vector offset
  17. B_HEAD:        equ    1AH        ; offset to BUFFER_HEAD
  18. B_TAIL:        equ    1CH        ; offset to BUFFER_TAIL
  19. KB_FLAG:    equ    17H        ; offset to KB_FLAG
  20.  
  21. main_:        jmp    init_code
  22. ;
  23. buffer:        dw    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  24.         dw    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  25.         dw    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  26.         dw    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  27.         dw    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  28.         dw    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  29.         dw    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  30.         dw    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  31.  
  32. head:        dw    buffer
  33. tail:        dw    buffer
  34.  
  35. ;
  36. ; the keystroke interrupt routine.  Interrcept the key interrupt, run
  37. ; it through the standard key input routine, and remove it from the buffer.
  38. ;
  39.  
  40. key_int:
  41.         cli
  42.         pushf            ; simulate an interrupt
  43. ;
  44. ; long call to F000:E987
  45. ;
  46.         db    9AH
  47.         dw    0E987H
  48.         dw    0F000H
  49. ;
  50.         push    bx
  51.         push    es
  52.         mov    bx,40H        ; BIOS data segment
  53.         mov    es,bx
  54.         mov    bx, es:[B_HEAD]    ; pointer to datum
  55.         cmp    bx, es:[B_TAIL]    ; test for character
  56.         je    k_esbx
  57.         mov    es:[B_TAIL], bx    ; clear the buffer
  58.         mov    bx, es:[bx]
  59.         push    si
  60.         mov    si, cs:tail
  61.         push    si        ; save tail value
  62.         add    si,2        ; test for full
  63.         cmp    si, offset buffer+256
  64.         jb    k_over1
  65.         mov    si, offset buffer
  66. k_over1:
  67.         cmp    si, cs:head
  68.         pop    si
  69.         je    k_siesbx    ; jump if buffer full
  70.         mov    cs:[si], bx    ; store the character
  71.         add    si,2
  72.         cmp    si, offset buffer+256
  73.         jb    k_over2
  74.         mov    si, offset buffer
  75. k_over2:
  76.         mov    cs:tail, si
  77. k_siesbx:
  78.         pop    si
  79. k_esbx:
  80.         pop    es        ; no character, return
  81.         pop    bx
  82.         iret
  83.  
  84. ;
  85. ; The request interrupt routine.
  86. ;
  87. ; simulate the BIOS routine
  88. ;    ah = 0    read next char
  89. ;    ah = 1    set Z flag on character status, ZF=1 if no char
  90. ;        ZF=0 and AX = char if char ready
  91. ;    ah = 2    shift status
  92.  
  93.  
  94. request:
  95.         sti
  96.         or    ah,ah
  97.         jz    do_read
  98.         dec    ah
  99.         jz    do_stat
  100.         dec    ah
  101.         jz    do_shift
  102.         iret
  103.  
  104. do_read:                ; return the next character
  105.         sti
  106.         nop
  107.         cli
  108.         mov    ax,cs:head
  109.         cmp    ax,cs:tail
  110.         je    do_read        ; loop until a character
  111.         push    bx
  112.         mov    bx,ax
  113.         mov    ax, cs:[bx]    ; ax gets the character
  114.         add    bx,2
  115.         cmp    bx, offset buffer+256
  116.         jb    r_over
  117.         mov    bx, offset buffer
  118. r_over:
  119.         cmp    bx, cs:tail
  120.         mov    cs:head, bx    ; new head
  121.         pop    bx
  122.         iret
  123.  
  124. do_stat:                ; return key status
  125.         cli
  126.         push    bx
  127.         mov    bx,cs:head
  128.         cmp    bx,cs:tail
  129.         mov    ax,cs:[bx]
  130.         pop    bx
  131.         sti
  132.         lret    2        ; throw out the flags
  133.  
  134. do_shift:
  135.         push    es
  136.         mov    ax,40H        ; BIOS data segment
  137.         mov    es,ax
  138.         mov    al, es:[KB_FLAG]
  139.         pop    es
  140.         iret
  141.         
  142. init_code:    cli            ; turn off interrupts for now
  143.         mov    ax,0
  144.         mov    es,ax        ; segment base for vectors
  145.         mov    es:[KEYINT], offset key_int
  146.         mov    es:[KEYINT+2], cs
  147.         mov    es:[REQINT], offset request
  148.         mov    es:[REQINT+2], cs
  149.         mov    cs:head, offset buffer
  150.         mov    cs:tail, offset buffer
  151.         sti
  152.         mov    ds:byte[1], 27H    ; change PCB terminate to resident
  153.         push    ds
  154.         mov    dx,0
  155.         push    dx
  156.         mov    dx, offset init_code+100H
  157.         lret            ; long return to the int 27H
  158.         end
  159.